/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-10-03     FuLei       the first version
 */

#include "rtthread.h"
#include "rtdevice.h"
#include "mygpio.h"
#include <board.h>
#include <Motor.h>
#include <task.h>
/**********************函数描述*********************
* 创建人:   傅雷
* 创建时间: 2023.9.29
* 功能描述: PA1_LED初始化
* 入口参数: void
* 函数返回: void
******************************************************/
void GPIO_LED_Init(void)
{
   //呼吸灯引脚初始化
    rt_pin_mode( GET_PIN(A,1) ,PIN_MODE_OUTPUT);

    rt_pin_write(GET_PIN(A,1) , PIN_HIGH);
}


//伺服电机使能引脚 PB12
void Motor_Init_Enable(void)
{
    rt_pin_mode( GET_PIN(B,12) ,PIN_MODE_OUTPUT);
    rt_pin_write(GET_PIN(B,12) , PIN_LOW);
}

//伺服电机失能引脚 PB13
void Motor_Init_Disable(void)
{
    rt_pin_mode( GET_PIN(B,13) ,PIN_MODE_OUTPUT);
    rt_pin_write(GET_PIN(B,13) , PIN_HIGH);
}

//光电传感器引脚初始化
void PhotoElectricity_Init(void)
{
    rt_pin_mode( GET_PIN(C,15) ,PIN_MODE_INPUT);
}

//急停初始化
void EmergencyStop(void)
{
    rt_pin_mode( GET_PIN(C,14) ,PIN_MODE_INPUT);
}


/********************函数描述*********************
* 创建人:   傅雷
* 创建时间: 2023.9.29
* 功能描述: PA1_LED 闪烁
* 入口参数: void
* 函数返回: void
******************************************************/
void LED_Flash(void)
{
    static rt_uint8_t io_flag = PIN_LOW;

    io_flag = !io_flag;     //标志位取反
    rt_pin_write(GET_PIN(A,1) , io_flag);
}

/**********************函数描述*********************
* 创建人:   傅雷
* 创建时间: 2023.10.1
* 功能描述: 力值传感器，标定，计算
* 入口参数: void
* 函数返回: void
******************************************************/

void GPIO_KEY_Init(void)
{
    //KEY引脚初始化
     rt_pin_mode( GET_PIN(B,5), PIN_MODE_INPUT);
     rt_pin_mode( GET_PIN(B,6), PIN_MODE_INPUT);
     rt_pin_mode( GET_PIN(B,7), PIN_MODE_INPUT);
     rt_pin_mode( GET_PIN(C,13), PIN_MODE_INPUT);
}

KeyDataFrm KeyData;
void GPIO_KEY_Scan(KeyDataFrm *pKey)
{
    static rt_uint8_t  downcnt = 0, upcnt=0, setcnt=0, switchcnt=0;
//-----------------------------------------------------
    // 右上 PB5
    // 1) up key-PB5
    if( rt_pin_read(GET_PIN(B,5)) == PIN_LOW  )
    {
        upcnt ++;
        if(upcnt >= 5)
        {
            pKey->up = 1;
            upcnt = 0;
            //按键标志信号
            if (pKey->upFlag) {
                pKey->upFlag = 0;
                Motor_Stop();
            }else {
                pKey->upFlag = 1;
                Motor_Start(StartData.period, StartData.pulse, PIN_HIGH, ENABLE);
            }
            rt_kprintf("key up %d\n",pKey->upFlag);
        }
    }
    else {
        pKey->up = 0;
        upcnt = 0;
    }
//-----------------------------------------------------
    // 右下 PB6
    // 2) down key-PB6
    if( rt_pin_read(GET_PIN(B,6)) == PIN_LOW  )
    {
        downcnt ++;
        if(downcnt >= 5)
        {
            pKey->down = 1;
            downcnt = 0;
            //按键标志信号
            if (pKey->downFlag) {
                pKey->downFlag = 0;
                Motor_Stop();
            }else {
                pKey->downFlag = 1;
                Motor_Start(StartData.period, StartData.pulse, PIN_HIGH, ENABLE);
            }
            rt_kprintf("key down %d\n",pKey->downFlag);
        }
    }
    else {
        pKey->down = 0;
        downcnt = 0;
    }
//-----------------------------------------------------
    // 左上 PB7
    // 3) set key-PB7
    if( rt_pin_read(GET_PIN(B,7) ) == PIN_LOW  )
    {
        setcnt ++;
        if(setcnt >= 5)
        {
            pKey->set = 1;
            setcnt = 0;
            rt_kprintf("key set\n");
        }
    }
    else {
        pKey->set = 0;
        setcnt = 0;
    }
//-----------------------------------------------------
    // 左下 PC13
    // 4) switch key-PC13
    if( rt_pin_read(GET_PIN(C,13) ) == PIN_LOW  )
    {
        switchcnt ++;
        if(switchcnt >= 5)
        {
            pKey->menu = 1;
            switchcnt = 0;
            //按键标志信号
            if (pKey->SwitchFlag) {
                pKey->SwitchFlag = 0;
            }else {
                pKey->SwitchFlag = 1;
                Motor_Stop(); //悬浮需要先停止电机
            }
            rt_kprintf("key switch %d\n",pKey->SwitchFlag);
        }
    }
    else {
        pKey->menu = 0;
        switchcnt = 0;
    }
}


